#coding:utf-8
'''
Created on 2017-04-20

@author: sandy
'''

import os
import json
import logging
from pycommon import Utils
from pycommon import RemoteManager

class Config(object):
    
    def __init__(self):
        self.rundir = os.path.dirname(__file__)
        self.server_ip = u""
        self.server_port_login = 8000
        self.server_port_game = 8001
        self.server_port_gate = 8002
        self.server_port_bill = 8003
        self.max_client = 100
        self.game_db_name = u"platform"
        self.game_db_host = u"192.168.1.100"
        self.game_db_port = 3306
        self.game_db_user = u"root"
        self.game_db_passwd = u"1111"
        self.game_db_linkcount = 2
        self.log_ip = u"127.0.0.1"
        self.log_port = 1240        
        self.telnet_ip = u"127.0.0.1"
        self.telnet_port = 8023
        self.web_ip = u"127.0.0.1"
        self.web_port = 8023
        self.redisdb_ip =u"192.168.1.100"
        self.redisdb_port=6379
        self.redisdb_db=0
        self.redisdb_passwd=u""

    
    def onRemoteConfig(self, cfgname, jf):
        ctype,cname = cfgname.split(u".")
        if ctype==u'server'+unicode(RemoteManager.instance.sid) and cname==u'serverconfig':
            self.loadFromJson(jf)
     

    def loadFromFile(self):
        cf = json.load(file(os.path.join(self.rundir + "/../config/","centerconfig.json")), encoding='utf-8')
        self.loadFromJson(cf)
       
    def loadFromJson(self, cf):
        logging.debug(u'CenterServer parse serverconfig.json')
        self.server_ip = cf.get("server_ip")
        self.server_port_login = cf.get("server_port_login")
        self.server_port_game = cf.get("server_port_game")
        self.server_port_gate = cf.get("server_port_gate")
        self.server_port_bill = cf.get("server_port_bill")
        self.max_client = cf.get("max_client")
        dbstr = cf.get("game_dbe")
        if dbstr==None:
            dbstr = cf.get("game_db")
        else:
            dbstr = Utils.dencryptDbStr(dbstr)
        dbparams = dbstr.split(u',')
        self.game_db_name = dbparams[2]
        self.game_db_host = dbparams[0]
        self.game_db_port = int(dbparams[1])
        self.game_db_user = dbparams[3]
        self.game_db_passwd = dbparams[4]
        self.game_db_linkcount = int(dbparams[5]) 

        self.log_ip = cf.get("log_ip")
        self.log_port = cf.get("log_port")                
        self.telnet_ip = cf.get("telnet_ip")
        self.telnet_port = cf.get("telnet_port")
        self.web_ip = cf.get("web_ip")
        self.web_port = cf.get("web_port")

        dbstr = cf.get("redis_db")
        dbparams = dbstr.split(u',')
        self.redisdb_ip =dbparams[0]
        self.redisdb_port=int(dbparams[1])
        self.redisdb_db= int(dbparams[2])
        self.redisdb_passwd=dbparams[3] if dbparams[3]!="0" else None
   
instance = Config()